package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysPostUnique;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.params.SysPostParams;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BasePlusController {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPostParams post) {
        PageDto pageDto = startPage();
        Page<SysPost> page = new Page(pageDto.getCurrentPage(), pageDto.getPageSize());
        Page<SysPost> list = postService.selectPostList(page,post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostParams post) throws IOException {
        Page<SysPost> page = new Page<>(1, -1);
        Page<SysPost> list = postService.selectPostList(page,post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list.getRecords(), "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
/*    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
        return AjaxResult.success(postService.selectPostById(postId));
    }*/
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{deptId}/{postCode}")
    public AjaxResult getInfo(@PathVariable Long deptId, @PathVariable String postCode) {
        return AjaxResult.success(postService.selectPost(deptId, postCode));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysPost post) {
        post.setCreateBy(SecurityUtils.getUsername());
        return R.ok(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysPost post) {
        post.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping()
    public R remove(@RequestBody List<SysPostUnique> sysRoleCodeList) {
        return toAjax(postService.deletePostByCodes(sysRoleCodeList));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/option-select/{deptId}")
    public AjaxResult optionSelect(@PathVariable Long deptId) {

        return AjaxResult.success(postService.selectPostByDeptId(deptId));
    }
}
